package org.boyan.cases;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.boyan.common.util.DateUtils;
import org.boyan.entity.cases.DemandInfo;
import org.boyan.utils.ExcelUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

public class ExcelToDemandInfo {

    //将传入的excel文件转换为DemandInfo
    public static DemandInfo getDemandInfo(Object fileObject) {
        Workbook workbook = null;
        if (fileObject instanceof MultipartFile) {
            MultipartFile file = (MultipartFile) fileObject;
            workbook = ExcelUtils.getWorkBook(file);
        } else if (fileObject instanceof File) {
            File file = (File) fileObject;
            workbook = ExcelUtils.getWorkBook(file);
        }
        DemandInfo demandInfo = new DemandInfo();
        try {
            Sheet sheet = workbook.getSheetAt(1);
            demandInfo.setDemandContact(sheet.getRow(0).getCell(1).getStringCellValue());
            demandInfo.setBussinessDepartment(sheet.getRow(0).getCell(3).getStringCellValue());
            demandInfo.setOaNo(sheet.getRow(1).getCell(1).getStringCellValue());
            CellType expectedDateCellType = sheet.getRow(1).getCell(3).getCellType();
            String expectedDate;
            if (expectedDateCellType == CellType.NUMERIC) {
                expectedDate = DateUtils.formatDate(sheet.getRow(1).getCell(3).getDateCellValue(), "yyyy-MM-dd");
            } else {
                expectedDate = sheet.getRow(1).getCell(3).getStringCellValue();
            }
            demandInfo.setExpectedDate(expectedDate);
            demandInfo.setDemandName(sheet.getRow(2).getCell(1).getStringCellValue());
            demandInfo.setOnlineNecessary(sheet.getRow(3).getCell(1).getStringCellValue());
            demandInfo.setUserQueryAction(sheet.getRow(3).getCell(3).getStringCellValue());
            demandInfo.setDataSourceSystem(sheet.getRow(4).getCell(1).getStringCellValue());
            demandInfo.setDataSourceSystemAbbr(sheet.getRow(4).getCell(3).getStringCellValue());
            demandInfo.setDataSourceSystemPrincipal(sheet.getRow(5).getCell(1).getStringCellValue());
            String operation = sheet.getRow(6).getCell(1).getStringCellValue();
            if ("是".equals(operation) || "Y".equals(operation)) {
                demandInfo.setIsOperation(1);
                CellType expectedOperationDateCellType = sheet.getRow(6).getCell(3).getCellType();
                String expectedOperationDate;
                if (expectedOperationDateCellType == CellType.NUMERIC) {
                    expectedOperationDate = DateUtils.formatDate(sheet.getRow(6).getCell(3).getDateCellValue(), "yyyy-MM-dd");
                } else {
                    expectedOperationDate = sheet.getRow(6).getCell(3).getStringCellValue();
                }
                demandInfo.setExpectedOperationDate(expectedOperationDate);
            } else {
                demandInfo.setIsOperation(0);
            }
            demandInfo.setInvokeSystem(sheet.getRow(7).getCell(1).getStringCellValue());
            demandInfo.setInvokeSystemAbbr(sheet.getRow(7).getCell(3).getStringCellValue());
            demandInfo.setInvokeSystemPrincipal(sheet.getRow(8).getCell(1).getStringCellValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return demandInfo;
    }


}
